//
//  QQFriendSectionHeaderView.m
//  Qya
//
//  Created by shoguncao on 12-9-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "QQFriendSectionHeaderView.h"

@implementation QQFriendSectionHeaderView

@synthesize titleLabel = _titleLabel;
@synthesize disclosureButton = _disclosureButton;
@synthesize section = _section;
@synthesize checkButton = _checkButton;
@synthesize isOpenLabel = _isOpenLabel;
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame title:(NSString*)title section:(NSInteger)sectionNumber isOpen:(BOOL)isOpen delegate:(id <SectionHeaderViewDelegate>)delegate
{
    self = [super initWithFrame:frame];
    
    if (self != nil)
    {
        _delegate = delegate;
        self.userInteractionEnabled = YES;
        
        // Create and configure the title label.
        _section = sectionNumber;
        CGRect titleLabelFrame = self.bounds;
        titleLabelFrame.origin.x += 65.0;
        titleLabelFrame.origin.y -= 10.0;
        titleLabelFrame.size.width -= 35.0;
        CGRectInset(titleLabelFrame, 0.0, 5.0);
        UILabel *label = [[UILabel alloc] initWithFrame:titleLabelFrame];
        label.text = title;
        label.font = [UIFont boldSystemFontOfSize:17.0];
        label.textColor = [UIColor blackColor];
        label.backgroundColor = [UIColor clearColor];
        [self addSubview:label];
        _titleLabel = label;
        
        // Create and configure the checkButton.
        CGRect checkButtonFrame = self.bounds;
        checkButtonFrame.origin.x += 16.0;
        checkButtonFrame.origin.y -= 10.0;
        checkButtonFrame.size.width = 20.0;
        CGRectInset(checkButtonFrame, 0.0, 5.0);
        UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkbox setFrame:checkButtonFrame];
        _checkButton = checkbox;
        [self setCheckBox];
        [self addSubview:_checkButton];
        
        // Create and configure the disclosure button.
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = titleLabelFrame;
        [button addTarget:self action:@selector(toggleOpen:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        _disclosureButton = button;
        
        // Create and configure the is open label
        CGRect isOpenLabelFrame = self.bounds;
        isOpenLabelFrame.origin.x += 45.0;
        isOpenLabelFrame.origin.y -= 10.0;
        isOpenLabelFrame.size.width -= 35.0;
        CGRectInset(isOpenLabelFrame, 0.0, 5.0);
        UILabel *isOpenLabel = [[UILabel alloc] initWithFrame:isOpenLabelFrame];
        isOpenLabel.text = (!isOpen)?@"》":@"︾";
        isOpenLabel.font = [UIFont boldSystemFontOfSize:17.0];
        isOpenLabel.textColor = [UIColor blackColor];
        isOpenLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:isOpenLabel];
        _isOpenLabel = isOpenLabel;
    }
    
    return self;
}

- (IBAction)toggleOpen:(id)sender {
    
    [self toggleOpenWithUserAction:YES];
}

- (void)toggleOpenWithUserAction:(BOOL)userAction {
    
    // Toggle the disclosure button state.
    self.disclosureButton.selected = !self.disclosureButton.selected;
    
    // If this was a user action, send the delegate the appropriate message.
    if (userAction) {
        if (self.disclosureButton.selected) {
            if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionOpened:)]) {
                [self.delegate sectionHeaderView:self sectionOpened:self.section];
            }
        }
        else {
            if ([self.delegate respondsToSelector:@selector(sectionHeaderView:sectionClosed:)]) {
                [self.delegate sectionHeaderView:self sectionClosed:self.section];
            }
        }
    }
}

- (void)setCheckBox
{
    [_checkButton setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal];
    [_checkButton setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected];
    
    [_checkButton addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside]; 
}

- (void)checkboxClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
}

@end
